package com.hyk.commonLib.common.view;


import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.view.Gravity;

import androidx.annotation.ColorInt;
import androidx.appcompat.widget.AppCompatButton;

import com.hyk.commonLib.R;
import com.hyk.commonLib.common.utils.ColorUtils;
import com.hyk.commonLib.common.utils.ResUtils;
import com.hyk.commonLib.common.utils.ScreenUtils;

/**
 * Created by 贺玉琨 on 2019/3/6 0006.
 */

public class RoundCornerButton extends AppCompatButton {
    @ColorInt
    protected int backColor;
    @ColorInt
    protected int backColorPress;
    @ColorInt
    protected int backColorDisable;
    protected int strokeWidth;
    protected int strokeColor;
    @ColorInt
    protected int borderColor;
    protected int borderWidth;
    protected float cornerRadiusLT;
    protected float cornerRadiusRT;
    protected float cornerRadiusLB;
    protected float cornerRadiusRB;
    protected boolean withRipple;

    public RoundCornerButton(Context context) {
        this(context, null);
    }

    public RoundCornerButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundCornerButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initBackground(attrs);
        refreshBackground();
    }

    private void initView() {
        setGravity(Gravity.CENTER);
    }

    protected void initBackground(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RoundCornerButton);
        withRipple = a.getBoolean(R.styleable.RoundCornerButton_withRipple, false);
        backColor = a.getColor(R.styleable.RoundCornerButton_backColor, ResUtils.getColor(R.color.default_btn_color, this));
        backColorPress = a.getColor(R.styleable.RoundCornerButton_backColorPress, ColorUtils.getDarkerColor(backColor));
        backColorDisable = a.getColor(R.styleable.RoundCornerButton_backColorDisable, ResUtils.getColor(R.color.default_btn_color_disable, this));
        float cornerRadius = a.getDimension(R.styleable.RoundCornerButton_cornerRadius, ScreenUtils.dp2px(5, this));
        cornerRadiusLT = a.getDimension(R.styleable.RoundCornerButton_cornerRadiusLT, cornerRadius);
        cornerRadiusRT = a.getDimension(R.styleable.RoundCornerButton_cornerRadiusRT, cornerRadius);
        cornerRadiusLB = a.getDimension(R.styleable.RoundCornerButton_cornerRadiusLB, cornerRadius);
        cornerRadiusRB = a.getDimension(R.styleable.RoundCornerButton_cornerRadiusRB, cornerRadius);
        borderColor = a.getColor(R.styleable.RoundCornerButton_borderColor, Color.WHITE);
        borderWidth = (int) a.getDimension(R.styleable.RoundCornerButton_borderWidth, 0);
        a.recycle();
    }

    public RoundCornerButton setBackColor(@ColorInt int backColor) {
        return setBackColor(backColor, ColorUtils.getDarkerColor(backColor));
    }

    public RoundCornerButton setBackColor(@ColorInt int backColor, @ColorInt int backColorPress) {
        this.backColor = backColor;
        this.backColorPress = backColorPress;
        refreshBackground();
        return this;
    }

    public void setBorderColor(@ColorInt int borderColor) {
        this.borderColor = borderColor;
        refreshBackground();
    }

    public void setBorderWidth(int borderWidth) {
        setBorderWidth(borderWidth, true);
        refreshBackground();
    }

    public void setBorderWidth(int borderWidth, boolean dpSize) {
        this.borderWidth = dpSize ? ScreenUtils.dp2px(borderWidth) : borderWidth;
        refreshBackground();
    }

    public void setBorder(@ColorInt int borderColor, int borderWidth) {
        setBorder(borderColor, borderWidth, true);
    }

    public void setBorder(@ColorInt int borderColor, int borderWidth, boolean dpSize) {
        this.borderColor = borderColor;
        this.borderWidth = dpSize ? ScreenUtils.dp2px(borderWidth) : borderWidth;
        refreshBackground();
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        refreshBackground();
    }

    protected void refreshBackground() {
        float[] cornerRadii = new float[]{
                cornerRadiusLT, cornerRadiusLT,
                cornerRadiusRT, cornerRadiusRT,
                cornerRadiusRB, cornerRadiusRB,
                cornerRadiusLB, cornerRadiusLB,
        };
        if (!isEnabled()) {
            GradientDrawable normalDrawable = new GradientDrawable();
            normalDrawable.setColor(backColorDisable);
            normalDrawable.setCornerRadii(cornerRadii);
            normalDrawable.setStroke(borderWidth, borderColor);
            setBackground(normalDrawable);
        } else {
            if (withRipple) {
                setBackground(getNormalDrawable());
            } else {
                StateListDrawable drawable = new StateListDrawable();
                drawable.addState(new int[]{android.R.attr.state_pressed}, getPressDrawable());
                drawable.addState(new int[]{}, getNormalDrawable());
                setBackground(drawable);
            }
        }

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            setTranslationZ(0);
//            setElevation(0);
//            //获取outLine，我们需要使用ViewoutLineProvider
//            ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
//                @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
//                @Override
//                public void getOutline(View view, Outline outline) {
////                    //修改outLine的形状，这里是设置分别设置左上右下，以及Radius
////                    outline.setRoundRect(0,0,view.getWidth(),view.getHeight(),30);
//                }
//            };
//            //将需要控件重写设置形状
//            setOutlineProvider(viewOutlineProvider);
//        }
    }
    protected Drawable getNormalDrawable() {
        float[] cornerRadii = new float[]{
                cornerRadiusLT, cornerRadiusLT,
                cornerRadiusRT, cornerRadiusRT,
                cornerRadiusRB, cornerRadiusRB,
                cornerRadiusLB, cornerRadiusLB,
        };
        GradientDrawable normalDrawable = new GradientDrawable();
        normalDrawable.setColor(backColor);
        normalDrawable.setCornerRadii(cornerRadii);
        normalDrawable.setStroke(borderWidth, borderColor);
        if (withRipple) {
            return new RippleDrawable(ColorStateList.valueOf(0x22000000), normalDrawable, null);
        }
        return normalDrawable;
    }

    protected Drawable getPressDrawable() {
        float[] cornerRadii = new float[]{
                cornerRadiusLT, cornerRadiusLT,
                cornerRadiusRT, cornerRadiusRT,
                cornerRadiusRB, cornerRadiusRB,
                cornerRadiusLB, cornerRadiusLB,
        };
        GradientDrawable pressDrawable = new GradientDrawable();
        pressDrawable.setColor(backColorPress);
        pressDrawable.setCornerRadii(cornerRadii);
        pressDrawable.setStroke(borderWidth, borderColor);
        if (withRipple) {
            return new RippleDrawable(ColorStateList.valueOf(0x22000000), pressDrawable, null);
        }
        return pressDrawable;
    }
}
